package vn.edu.ptithcm.pim.dao;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.LockMode;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import vn.edu.ptithcm.pim.dom.TaskLog;

@SuppressWarnings("rawtypes")
public class TaskLogDao extends HibernateDaoSupport {
	Logger log = Logger.getLogger(TaskLogDao.class);

	public void save(TaskLog taskLog) {
		try {
			Transaction tx = getSession().beginTransaction();
			getHibernateTemplate().save(taskLog);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void saveOrUpdate(TaskLog taskLog) {
		try {
			Transaction tx = getSession().beginTransaction();
			getHibernateTemplate().saveOrUpdate(taskLog);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void lock(TaskLog taskLog) {
		try {
			Transaction tx = getSession().beginTransaction();
			getHibernateTemplate().lock(taskLog, LockMode.NONE);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void delete(TaskLog TaskLog) {
		try {
			Transaction tx = getSession().beginTransaction();
			getHibernateTemplate().delete(TaskLog);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public TaskLog merge(TaskLog taskLog) {
		try {
			return (TaskLog) getHibernateTemplate().merge(taskLog);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public TaskLog findById(java.lang.Long id) {
		try {
			return (TaskLog) getHibernateTemplate().get(TaskLog.class, id);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List findByExample(TaskLog taskLog) {
		try {
			return getHibernateTemplate().findByExample("TaskLog", taskLog);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List getAll() {
		try {
			return getHibernateTemplate().loadAll(TaskLog.class);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List getLogs(Long taskId) {
		try {
			StringBuilder sb = new StringBuilder();
			sb.append("FROM TaskLog as log WHERE log.task.taskid='");
			sb.append(taskId).append("' ORDER BY log.time asc");
			return getHibernateTemplate().find(sb.toString());
		} catch (RuntimeException re) {
			throw re;
		}
	}
}
